<?php 
require 'common.inc.php';
mobile_login();
//$_groupid >5 or mobile_msg('普通会员不能进入交易大厅！',WAP_URL.'member/');
require DT_ROOT.'/module/'.$module.'/common.inc.php';
require DT_ROOT.'/include/post.func.php';

if($action == 'doup' && $submit){
    $amount or message_json('请填写充值金额');
	$paytype or message_json('请选择支付方式');
    $amount = dround($amount);
    //($amount>=100 && $amount<=100000) or message_json('汇款金额应在100至100000之间');
    $paytype = $paytype;
	if($_userid)
	{
		$paytime = date('Y-m-d H:i:s');
		$ordernumber = str_pad($_userid,9,"0",STR_PAD_LEFT).date('YmdHis',strtotime($paytime)).mt_rand(1000,9999).mt_rand(0,9).mt_rand(0,9).mt_rand(0,9);
	}
	else
	{
		message_json('请登录！',1,'index.php');
	}
	$payorder = $db->get_one("SELECT id FROM {$DT_PRE}pay WHERE ordernumber='$ordernumber'");
	if(!isset($payorder['id']) || !$payorder['id']) 
	{
		$db->query("INSERT INTO {$db->pre}pay (username,amount,paytime,type,status,ordernumber,ip) VALUES ('$_username','$amount','$paytime','$paytype',2,'$ordernumber','$DT_IP')");
		message_json('提交成功',1,'wapay.php?action=orderpay&order='.$ordernumber);
	}
    else
	{
		message_json('请重新提交!');
	}
	
}

if($action == 'orderpay'){
	$ordernumber = isset($_GET['order'])?$_GET['order']:'';
	if(!$ordernumber)
	{
		message_json('提交参数错误!');
	}
   $payorder = $db->get_one("SELECT * FROM {$DT_PRE}pay WHERE ordernumber='$ordernumber'");
   $head_title = '充值支付订单';
   include template('wapayorder', WAP_MOD_TP);
}

if($action == 'onlinepayorder'){
	$ordernumber = isset($_GET['order'])?$_GET['order']:'';
	$money = isset($_GET['money'])?$_GET['money']:'';
	$username = isset($_GET['username'])?trim($_GET['username']):'';
	$type = isset($_GET['type'])?trim($_GET['type']):'';
	$id = $_GET['id']?$_GET['id']:'';
	if(!$ordernumber||!$money||!$username)
	{
		message_json('提交参数错误!');
	}
	include DT_ROOT.'/api/payphp/init.php';
	// 加载配置参数
	$config = require(DT_ROOT.'/api/payphp/test/config.php');
	
	if($type==1)//微信支付
	{
		//echo '微信支付';exit;
		// 支付参数
		$options = [
			'out_trade_no'     => time(), // 订单号
			'total_fee'        => '101', // 订单金额，**单位：分**
			'body'             => '订单描述', // 订单描述
			'spbill_create_ip' => '127.0.0.1', // 支付人的 IP
			'notify_url'       => 'http://localhost/notify.php', // 定义通知URL
		];

		$return_url = 'http://localhost/pay_return.php';

		// 实例支付对象
		$pay = new \Pay\Pay($config);

		try {
			$result = $pay->driver('wechat')->gateway('wap')->apply($options, $return_url);
			echo '<pre>';
			var_export($result);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
	}
	else//支付宝支付
	{
		//dump($_GET);
		$user = $db->get_one("SELECT userid FROM {$DT_PRE}member WHERE username='$username'");
		//echo "SELECT userid FROM {$DT_PRE}member WHERE username='$username'";exit;
		if(!isset($user['userid']) || !$user['userid']) 
		{
			message_json('请登录!');
		}

		// 参考请求参数  https://docs.open.alipay.com/270/alipay.trade.page.pay
		$options = [
			'out_trade_no' => $ordernumber, // 商户订单号
			'total_amount' => $money, // 支付金额
			'subject'      => '激活金充值', // 支付订单描述
		];

		// 参考公共参数  https://docs.open.alipay.com/270/alipay.trade.page.pay
		$config['alipay']['notify_url'] = 'http://www.gddcjt.cn/api/payphp/test/alipay-notify.php';
		$config['alipay']['return_url'] = 'http://www.gddcjt.cn/api/payphp/test/alipay-success.php';
		//dump($config);
		// 实例支付对象
		$pay = new \Pay\Pay($config);
		//echo '<pre>';
		//print_r($pay);exit;
		try {
			echo $pay->driver('alipay')->gateway('wap')->apply($options);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
	}
	//dump($_GET);
	/*$table = $DT_PRE.'pay';
	$money = round($money/1,2);
	$state = 1;//该值为模拟值
	//echo $money;exit;
	if($state == 1) {
		money_add($user['username'], $money, 'username','guquan');//增加注册劵
		money_record($user['username'], $money, '激活金账户', $_username, '核实汇款成功', '汇款金额：￥'.$money, 'username','guquan');
	}else if($state == 2) {
		message_json('提交参数错误!');
	}
	//更新付款状态
	$db->query("UPDATE {$table} SET status=$state,out_trade_no='$ordernumber' WHERE ordernumber='$ordernumber'");
	*/
	//print_r('充值成功');
	exit;
}

//支付成功跳转的页面
if($action == 'onlinepayok'){
	$ordernumber = isset($_GET['order'])?$_GET['order']:'';
	$money = isset($_GET['money'])?$_GET['money']:'';
	$username = isset($_GET['username'])?trim($_GET['username']):'';
	$id = $_GET['id']?$_GET['id']:'';
	if(!$ordernumber||!$money||!$username)
	{
		message_json('提交参数错误!');
	}
	//dump($_GET);
	$user = $db->get_one("SELECT userid FROM {$DT_PRE}member WHERE username='$username'");
	//echo "SELECT userid FROM {$DT_PRE}member WHERE username='$username'";exit;
	if(!isset($user['userid']) || !$user['userid']) 
	{
		message_json('请登录!');
	}
	//dump($_GET);
	$table = $DT_PRE.'pay';
	$money = round($money/1,2);
	$state = 1;//该值为模拟值
	//echo $money;exit;
	if($state == 1) {
		money_add($user['username'], $money, 'username','guquan');//增加注册劵
		money_record($user['username'], $money, '激活金账户', $_username, '核实汇款成功', '汇款金额：￥'.$money, 'username','guquan');
	}else if($state == 2) {
		message_json('提交参数错误!');
	}
	//更新付款状态
	$db->query("UPDATE {$table} SET status=$state,out_trade_no='$ordernumber' WHERE ordernumber='$ordernumber'");
	print_r('充值成功');exit;
}

if($action == 'index'){
	$MOD['cash_enable'] or mobile_msg($L['feature_close'], $MOD['linkurl']);
	$head_title = '充值支付';
	include template('wapay', WAP_MOD_TP);
}

if($action == 'list'){
	include template('wapaylist', WAP_MOD_TP);
}

if($action == 'mbget'){
	$table = $DT_PRE.'pay';
	$josndata['list'] = array();
    $condition = "username='$_username' and status=1";
    $r = $db->get_one("SELECT COUNT(*) AS num FROM {$table} WHERE $condition");
    $total = $r['num'];
    $pagesize = 10;
    $offset = $pagesize*($page-1);
    $result = $db->query("SELECT * FROM {$table} WHERE $condition ORDER BY id DESC LIMIT $offset,$pagesize");
    while($r = $db->fetch_array($result)) {
        //$r['paytime'] = timetodate($r['addtime'], 5);
        $LS['id'] = $r['id'];
        $LS['username'] = $r['username'];
        $LS['money'] = $r['amount'];
        $LS['paytime'] = $r['paytime'];
        $LS['type'] = $r['type']==1?'微信支付':'支付支付';
        $LS['status'] = $r['status']==1?'支付成功':'支付失败';
        $LS['out_trade_no'] = $r['out_trade_no'];
        $LS['ordernumber'] = $r['ordernumber'];
        $LS['buyer_id'] = $r['buyer_id'];
        $josndata['list'][] = $LS;
    }
    $josndata['total'] = $total;
    $josndata['pagesize'] = $pagesize;
    $josndata['url'] = DT_PATH;
	//dump($josndata);
    $josndata = json_encode($josndata);
    exit('{"status":1,"result":'.$josndata.'}');
}

function dump($arr)
{
	echo "<pre>";
	print_r($arr);exit;
}
?>